﻿namespace Admin.NET.Application.Service;

/// <summary>
/// 打印模板和动态数据源
/// </summary>
public class BasPrintService : BaseService<BasPrint>
{
    SqlSugarRepository<BasPrintDraft> _repBasPrintDraft;
    DataBaseManager _dbManager;

    public BasPrintService(IServiceScopeFactory scopeFactory, SqlSugarRepository<BasPrintDraft> repBasPrintDraft, DataBaseManager dbManager) : base(scopeFactory)
    {
        _repBasPrintDraft = repBasPrintDraft;
        _dbManager = dbManager;
    }

    #region 正式
    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<BasPrint>> Page(BaseKeywordPageInput input)
    {
        return await Queryable
           .WhereIF(input.Keyword.HasValue(), it => it.code == input.Keyword || it.title == input.Keyword)
           .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 通过打印类型和入参获取数据源和模板
    /// </summary>
    public async Task<BasPrintGetDataAndTemplateOutput> QueryDataAndTemplate(BasPrintGetDataAndTemplateInput input)
    {
        var entity = await Queryable.Where(it => it.code == input.Code).FirstAsync();
        if (entity == null)
        {
            throw Oops.Oh("打印代码不存在");
        }

        List<SugarParameter> parameter = new List<SugarParameter>();
        parameter.Add(new SugarParameter("@p1", input.P1));
        parameter.Add(new SugarParameter("@p2", input.P2));
        parameter.Add(new SugarParameter("@p3", input.P3));

        var t1 = _dbManager.Query2List(entity.sql1, parameter);
        var t2 = _dbManager.Query2List(entity.sql2, parameter);
        var t3 = _dbManager.Query2List(entity.sql3, parameter);
        var t4 = _dbManager.Query2List(entity.sql4, parameter);
        var t5 = _dbManager.Query2List(entity.sql5, parameter);

        return new BasPrintGetDataAndTemplateOutput()
        {
            Id = entity.Id,
            Template = entity.template,
            Data = new BasPrintDataOutput()
            {
                T1 = t1,
                T2 = t2,
                T3 = t3,
                T4 = t4,
                T5 = t5,
            }
        };
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public BasPrint Get(long id)
    {
        return GetById<BasPrint>(id);
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public long Add(BasPrintAddInput input)
    {
        var hasValue = Queryable.Where(it => it.code == input.code).Any();
        if (hasValue) { throw Oops.Oh("代码已存在,不能重复"); }

        return AddWithInput(input);
    }

    /// <summary>
    /// 更新代码
    /// </summary>
    public bool UpdateCode(BasPrintUpdateInput input)
    {
        var hasValue = Queryable.Where(it => it.code == input.code && it.Id != input.Id).Any();
        if (hasValue) { throw Oops.Oh("代码已存在,不能重复"); }
        return _rep.Update(u => new() { category = input.category, code = input.code, }, u => u.Id == input.Id);
    }

    #endregion

    #region 草稿

    /// <summary>
    /// 获取草稿,如果不存在则创建一个新的并返回
    /// </summary>
    public async Task<BasPrintDraft> GetDraft(long id)
    {
        var obj = await _repBasPrintDraft.Queryable.InSingleAsync(id);
        if (obj == null)
        {
            var bs = GetById<BasPrint>(id);
            obj = bs.Adapt<BasPrintDraft>();
            obj.UpdateTime = DateTime.Now;
            _repBasPrintDraft.Insert(obj);
        }
        return obj;
    }

    /// <summary>
    /// 更新草稿,不更新code
    /// </summary>
    public async Task<bool> UpdateDraft(BasPrintDraft input)
    {
        return await _repBasPrintDraft.UpdateAsync(input);
    }

    public async Task<BasPrintGetDataAndTemplateOutput> QueryDataAndTemplateForDraft(BasPrintDraftQueryInput input)
    {
        var entity = await _repBasPrintDraft.Queryable.Where(it => it.Id == input.Id).FirstAsync();
        if (entity == null)
        {
            throw Oops.Oh("草稿不存在");
        }

        List<SugarParameter> parameter = new List<SugarParameter>();
        parameter.Add(new SugarParameter("@p1", input.P1));
        parameter.Add(new SugarParameter("@p2", input.P2));
        parameter.Add(new SugarParameter("@p3", input.P3));

        var t1 = _dbManager.Query2List(entity.sql1, parameter);
        var t2 = _dbManager.Query2List(entity.sql2, parameter);
        var t3 = _dbManager.Query2List(entity.sql3, parameter);
        var t4 = _dbManager.Query2List(entity.sql4, parameter);
        var t5 = _dbManager.Query2List(entity.sql5, parameter);

        return new BasPrintGetDataAndTemplateOutput()
        {
            Id = entity.Id,
            Template = entity.template,
            Data = new BasPrintDataOutput()
            {
                T1 = t1,
                T2 = t2,
                T3 = t3,
                T4 = t4,
                T5 = t5,
            }
        };
    }

    /// <summary>
    /// 确认草稿: 把草稿内容更新到BasPrint表中
    /// </summary>
    public async Task<bool> ConfirmDraft(BaseIdInput input)
    {
        var draftEntity = await _repBasPrintDraft.Queryable.InSingleAsync(input.Id);
        var entity = GetById<BasPrint>(input.Id);
        BasPrint basPrint = draftEntity.Adapt<BasPrint>();
        basPrint.category = entity.category; // 代码不更新
        basPrint.code = entity.code; // 代码不更新
        return UpdateWithInput(basPrint);
    }

    #endregion

}